package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    public int save(User user) {
        return userMapper.insert(user);
    }

    public User getByUserId(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public int updateByUserId(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    public User selectByEmailAndPassword(String email, String password) {
        return userMapper.selectByEmailAndPassword(email,password);
    }
    public void register(User user){
        //检查邮箱是否已经被注册了
        User temp = userMapper.selectByEmailAndPassword(user.getEmail(), null);
        if (temp != null) {
            // 邮箱已经存在就派出异常
            throw new RuntimeException("亲,邮箱已经被注册!");
        }
        //设置头像和背景图片
        user.setHeadImgUrl("/img/user/head.jpg");
        user.setCoverImgUrl("/img/user/bg.jpeg");
        //保存用户信息
        userMapper.insert(user);
    }

    public User login(String email, String password) {
        User user = userMapper.selectByEmailAndPassword(email, password);
        if (user == null) {
           throw  new RuntimeException("邮箱密码不匹配!");
        }
        /*在用户登陆成功后就把当前用户设置在session中*/
        UserContext.setUser(user);
        return user;
    }

    public PageInfo<User> query(UserQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public User selectCoverUrl(User user) {
        return userMapper.selectCoverUrl(user);
    }

    public Map<String, Object> getCounts(String keyword) {
        return userMapper.getCounts(keyword);
    }



    public User selectById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public Map<String, Object> selectAandF(Long id) {
        Map<String, Object> map = userMapper.selectAandF(id);
        System.out.println(map);
        return map;
    }

    public void saveAandF(Long userId,Long hearImgUrlId) {
        userMapper.saveAttention(userId,hearImgUrlId);
        userMapper.saveFancier(userId,hearImgUrlId);
    }

    public void updateAandF(Long userId, Long hearImgUrlId) {
        userMapper.updateAttention(userId,hearImgUrlId);
        userMapper.updateFancier(userId,hearImgUrlId);
    }

    public Map<String,Object> selectStateByUserId(Long userId, Long hearImgUrlId) {
        return userMapper.selectStateByUserId( userId,  hearImgUrlId);
    }


    public User queryforUserProfilers(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    public void saveUserAndTicket(Order order) {
        userMapper.saveUAndTRelation(order);
    }

    public PageInfo<Order> selectOrderByUserId(UserQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Order> list = userMapper.selectForUserOrder(qo);
        return new PageInfo(list);
    }

}
